package log

import (
	"fmt"
	"os"
)

type LoggerFile struct {
	pool chan string
}

func (logger *LoggerFile) Init(path string) {
	logger.pool = make(chan string, 1)
	fmt.Println(path)
	go logger.Sender(logger.pool)
}

func (logger *LoggerFile) Log(message string) {
	logger.pool <- message
}

func (logger *LoggerFile) Sender(pool chan string) {
	fmt.Println("func Sender()")
	var message string
	for {
		message = <-pool
		// 不要漏了os.O_RDWR，否则会报：write ./business.log: bad file descriptor
		writeFile, errOpenFile := os.OpenFile("./business.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
		if errOpenFile != nil {
			fmt.Println(errOpenFile)
		}
		_, errWriteFile := writeFile.Write([]byte(message+"\n"))
		if errWriteFile != nil {
			fmt.Println(errWriteFile)
		}
		writeFile.Close()
	}
}
